package cn.jbolt.common.base;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;

import cn.jbolt.common.util.AESUtils;

/**
 * Created by xyzx1 on 2017-07-13.
 */
public class DecoderRequest extends HttpServletRequestWrapper {
    private static final Logger logger = LoggerFactory.getLogger(DecoderRequest.class);
    private JSONObject jsonObj;
    private Prop p= PropKit.use("config.properties");

    public DecoderRequest(HttpServletRequest request) {
        super(request);
        decodeRequestParams(request);
        BaseReqMsg reqMsg=new BaseReqMsg();
        if(jsonObj!=null){
            reqMsg.putAll(jsonObj);
        }
        request.setAttribute("reqMsg",reqMsg);
        request.setAttribute("params",jsonObj);
    }

    @Override
    public String getParameter(String name) {
    	if(jsonObj != null) {    		
    		return jsonObj.getString(name);
    	}else {
    		return super.getParameter(name);
    	}
    }

    @Override
    public Map<String, String[]> getParameterMap() {
        if(jsonObj!=null){
            return JSONObject.parseObject(jsonObj.toJSONString(),Map.class);
        }else{
            return  super.getParameterMap();
        }

    }


    /**
     * The default behavior of this method is to return
     * getParameterValues(String name) on the wrapped request object.
     *
     * @param name
     */
    @Override
    public String[] getParameterValues(String name) {
        String[] result=null;
        if(jsonObj!=null){
            try {
                JSONArray array=jsonObj.getJSONArray(name);
                result=JSONObject.parseObject(array.toJSONString(),String[].class);
                return result;
            } catch (Exception e) {
                result=super.getParameterValues(name);
            }
        }else{
            result= super.getParameterValues(name);
        }
        return  result;

    }

    @Override
    public String getQueryString() {
        //return super.getQueryString();
        Set<String> keySet=jsonObj.keySet();
        Iterator<String> it=keySet.iterator();
        StringBuffer sb=new StringBuffer();
        while (it.hasNext()) {
            String key = it.next();
            sb.append("&"+key+"="+jsonObj.getString(key));
        }
        return sb.substring(1);
    }

    private void decodeRequestParams(HttpServletRequest request){
        String encodeParams=request.getParameter("q");
            String decodeParams= AESUtils.decrypt(encodeParams,p.get("AES_CIPHER_CODE"));
            JSONObject json= JSONObject.parseObject(decodeParams,JSONObject.class);
            jsonObj=json;
            //请求参数密文，解析参数后如果不需要保留原密文，这行代码可以移除
            //jsonObj.put("q",encodeParams);
        }
}
